package com.btnhom;

import java.awt.EventQueue;
import java.awt.Font;
import java.text.ParseException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class F_Calculator extends JFrame {

	private JPanel contentPane;
	private IntegerTextField txtNumber;
	private double a,b;
	private int type,prevtype;
	private StringBuilder str;
	private JButton btt_Sqrt;
	private boolean init;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					
					F_Calculator frame = new F_Calculator();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 * @throws ParseException 
	 */
	public F_Calculator() throws ParseException {
		a = 0; 
		b = 0;
		init=true;
		type=-1;
		prevtype=-1;
		str=new StringBuilder();
		setTitle("Calculator");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 360, 325);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JButton btt_0 = new JButton("0");
		btt_0.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				str.append("0");
				txtNumber.setText(str.toString());
			}
		});
		btt_0.setBounds(10, 244, 121, 31);
		contentPane.add(btt_0);
		
		JButton btt_1 = new JButton("1");
		btt_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				str.append("1");
				txtNumber.setText(str.toString());
			}
		});
		btt_1.setBounds(10, 201, 54, 31);
		contentPane.add(btt_1);
		
		JButton btt_2 = new JButton("2");
		btt_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				str.append("2");
				txtNumber.setText(str.toString());
			}
		});
		btt_2.setBounds(74, 201, 54, 31);
		contentPane.add(btt_2);
		
		JButton btt_3 = new JButton("3");
		btt_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				str.append("3");
				txtNumber.setText(str.toString());
			}
		});
		btt_3.setBounds(141, 201, 54, 31);
		contentPane.add(btt_3);
		
		JButton btt_4 = new JButton("4");
		btt_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				str.append("4");
				txtNumber.setText(str.toString());
			}
		});
		btt_4.setBounds(10, 159, 54, 31);
		contentPane.add(btt_4);
		
		JButton btt_5 = new JButton("5");
		btt_5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				str.append("5");
				txtNumber.setText(str.toString());
			}
		});
		btt_5.setBounds(74, 159, 54, 31);
		contentPane.add(btt_5);
		
		JButton btt_6 = new JButton("6");
		btt_6.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				str.append("6");
				txtNumber.setText(str.toString());
			}
		});
		btt_6.setBounds(141, 159, 54, 31);
		contentPane.add(btt_6);
		
		JButton btt_7 = new JButton("7");
		btt_7.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				str.append("7");
				txtNumber.setText(str.toString());
			}
		});
		btt_7.setBounds(10, 119, 54, 31);
		contentPane.add(btt_7);
		
		JButton btt_8 = new JButton("8");
		btt_8.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				str.append("8");
				txtNumber.setText(str.toString());
			}
		});
		btt_8.setBounds(74, 119, 54, 31);
		contentPane.add(btt_8);
		
		JButton btt_9 = new JButton("9");
		btt_9.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				str.append("9");
				txtNumber.setText(str.toString());
			}
		});
		btt_9.setBounds(141, 119, 54, 31);
		contentPane.add(btt_9);
		
		JButton btt_CE = new JButton("CE");
		btt_CE.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
			}
		});
		btt_CE.setBounds(74, 77, 54, 31);
		contentPane.add(btt_CE);
		
		JButton btt_C = new JButton("C");
		btt_C.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				a = 0;
				b = 0;
				str=new StringBuilder();
				txtNumber.setText("");
				txtNumber.requestFocus();
				init=true;
			}
		});
		btt_C.setBounds(141, 77, 54, 31);
		contentPane.add(btt_C);
		
		JButton btt_1x = new JButton("1/x");
		btt_1x.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				a=Double.parseDouble(txtNumber.getText());
				a=1/a;
				txtNumber.setText(String.valueOf(CheckInteger(a)));
				str=new StringBuilder();
			}
		});
		btt_1x.setBounds(279, 159, 54, 31);
		contentPane.add(btt_1x);
		
		JButton btt_dec = new JButton(".");
		btt_dec.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String number = txtNumber.getText();
				if(!number.contains(".") && !number.equals("")){				
					str.append(".");
					txtNumber.setText(str.toString());
				}
			}
		});
		btt_dec.setBounds(141, 244, 54, 31);
		contentPane.add(btt_dec);
		
		JButton bttNP = new JButton("+/-");
		bttNP.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Double number = Double.parseDouble(txtNumber.getText());
				number = number*(-1);
				txtNumber.setText(String.valueOf(CheckInteger(number)));
				str = new StringBuilder();
				str.append(txtNumber.getText());
			}
		});
		bttNP.setBounds(211, 77, 54, 31);
		contentPane.add(bttNP);
		
		JButton btt_D = new JButton("/");
		btt_D.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				type = 4;
				Cal(type);
			}
		});
		btt_D.setBounds(211, 119, 54, 31);
		contentPane.add(btt_D);
		
		JButton btt_P = new JButton("+");
		btt_P.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				type = 1;
				Cal(type);
			}
		});
		btt_P.setBounds(211, 244, 54, 31);
		contentPane.add(btt_P);
		
		JButton btt_S = new JButton("-");
		btt_S.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				type = 2;
				Cal(type);
			}
		});
		btt_S.setBounds(211, 201, 54, 31);
		contentPane.add(btt_S);
		
		JButton btt_M = new JButton("*");
		btt_M.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				type = 3;
				Cal(type);
			}
		});
		btt_M.setBounds(211, 159, 54, 31);
		contentPane.add(btt_M);
		
		btt_Sqrt = new JButton("\u221A");
		btt_Sqrt.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				a = Double.parseDouble(txtNumber.getText());
				a = Math.sqrt(a);
				txtNumber.setText(String.valueOf(CheckInteger(a)));
				str = new StringBuilder();
			}
		});
		btt_Sqrt.setBounds(279, 77, 54, 31);
		contentPane.add(btt_Sqrt);
		
		JButton btt_Percent = new JButton("%");
		btt_Percent.setBounds(279, 119, 54, 31);
		contentPane.add(btt_Percent);
		
		JButton btt_E = new JButton("=");
		btt_E.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Cal(type);
				a = 0;
				init=true;
			}
		});
		btt_E.setBounds(279, 201, 54, 74);
		contentPane.add(btt_E);
		
		txtNumber = new IntegerTextField();
		txtNumber.setText("0");
		txtNumber.setHorizontalAlignment(SwingConstants.RIGHT);
		txtNumber.setFont(new Font("Tahoma", Font.BOLD, 14));
		txtNumber.setBounds(10, 11, 324, 55);
		contentPane.add(txtNumber);
		
	}
	private Object CheckInteger(double number){
		boolean isInt = number == (int)number;
		if(isInt)
			return (int)number;
		else
			return number;
	}
	
	private void Cal(int _type){
		str=new StringBuilder();
		if(prevtype!=type && !init)
		{
			//a=b;
			//txtNumber.setText(String.valueOf(CheckInteger(a)));
			Cal(prevtype);
			prevtype=type;
			return;
		}
		switch (_type) {
		case 1:
			prevtype=1;
			if(!init){
				a += Double.parseDouble(txtNumber.getText());				
				txtNumber.setText(String.valueOf(CheckInteger(a)));
			}
			else{
				a = Double.parseDouble(txtNumber.getText());
				init=false;
			}
			b=a;
			break;
		case 2:
			prevtype=2;
			if(!init){
				a -= Double.parseDouble(txtNumber.getText());
				txtNumber.setText(String.valueOf(CheckInteger(a)));
			}
			else{
				a = Double.parseDouble(txtNumber.getText());
				init=false;
			}
			b=a;
				break;
				
		case 3:
			if(!init){
			a *= Double.parseDouble(txtNumber.getText());
			
			}
			else{
				a = Double.parseDouble(txtNumber.getText());
				init=false;
			}
			txtNumber.setText(String.valueOf(CheckInteger(a)));
			break;
		case 4:
			if(!init){
				a /=Double.parseDouble(txtNumber.getText());
				b=a;
				init=true;
			}
			else{
				a = Double.parseDouble(txtNumber.getText());
				init=false;
			}
			txtNumber.setText(String.valueOf(CheckInteger(b)));
			break;
		}
	}

	
}